<?php

namespace app\admin\controller\mall\shop;

use app\admin\controller\mall\Base;
use think\Db;

/**
 * 店铺
 *
 * @icon fa fa-circle-o
 */
class Shop extends Base
{
    
    /**
     * Shops模型对象
     * @var \app\common\model\mall\shop\Shop
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\mall\shop\Shop;
        $this->view->assign("stateList", $this->model->getStateList());
        $this->view->assign("statusList", $this->model->getStatusList());

    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->alias("s")
            ->join("__MALL_SHOP_EXTRA__ e","s.id=e.shop_id","INNER")
            ->where("s.id",$ids)->field("s.*,e.*")->find();

        if (!$row) $this->error(__('No Results were found'));

        $accredList = db("mall_shop_accred")->select();
        $this->assign('accredList',$accredList);
        //获取店铺认证标志IDS
        $accreds = model("AccredAccess","model\mall\shop")
            ->where("shop_id","=",$ids)
            ->column("accred_id,status");
        $row['accreds'] = $accreds;
        $accred_ids="";
        foreach($accreds as $key=>$val){
            $accred_ids .= $key . ",";
        }
        $row['accred_ids'] = mb_substr($accred_ids,0,-1);
        //获取标志状态
        $accredStatusList = model("AccredAccess","model\mall\shop")->getStatusList();
        $this->assign('accredStatusList',$accredStatusList);

        $admin_ids = $this->getDataLimitAdminIds();
        if (is_array($admin_ids)){
            if (!in_array($row[$this->dataLimitField], $admin_ids)){
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()){
            $params = $this->request->post("row/a");
            if ($params){
                Db::startTrans();
                try{
                    //是否采用模型验证
                    if ($this->modelValidate){
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);

                    //保存认证申请
                    $accredAccess = new \app\common\model\mall\shop\AccredAccess;
                    $accredAccess->where(["shop_id"=>$ids,"accred_id"=>["in",$params["accred_ids"]]])->delete();
                    $accreds = [];
                    foreach($params['accred_ids'] as $v){
                        $accreds[] = ["accred_id"=>$v,"shop_id"=>$ids,"status"=>isset($params['accreds'][$v])?$params['accreds'][$v]:"audit"];
                    }
                    $result2 = $accredAccess->saveAll($accreds);

                    if ($result||$result2 !== false){
                        Db::commit();
                        $this->success();
                    }else{
                        Db::rollback();
                        $this->error($row->getError());
                    }
                }
                catch (\think\exception\PDOException $e){
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $extraModel = new \app\common\model\mall\shop\Extra;
        $this->view->assign("businesslicencetypeList", $extraModel->getBusinesslicencetypeList());
        $this->view->assign("legalcertificatetypeList", $extraModel->getLegalcertificatetypeList());
        $this->view->assign("taxpayertypeList", $extraModel->getTaxpayertypeList());
        $goodsCatList = model('Cat','model\mall\goods')->listQuery(0);
        $this->assign('goodsCatList',$goodsCatList);

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    public function selectpage()
    {
        return parent::selectpage(); // TODO: Change the autogenerated stub
    }
}
